
#ifndef __game_replayH__
#define __game_replayH__

#include "game.h"
#include "data.h"

namespace WtfEngine {
	
	class Replay: public DataObject {
	public:
		typedef std::pair<tGameTime, NonCopyBuffer<>::tRef> tFrame;
		typedef std::vector<tFrame> tFrameSet;

	private:
		tFrameSet mFrames;
		tGameTime mPosition;
		int mPositionIndex;
		unsigned mMaxFrames;
		NonCopyBuffer<>::tRef mrOriginalState;

	public:
		inline Replay( const StringPool::Entry& sName ): DataObject(sName) {};

		void StartPlayback();
		void StopPlayback();
		void Seek(tGameTime t);
		void PlayFrame();

		void StartRecording( unsigned maxFrames );
		void StopRecording();
		void SaveFrame();
		void SaveToFile( const String& sFile );

		void Reset();
		void Clear();

		inline tGameTime getLength() const {
			return mFrames.empty() ? 0.0 : mFrames.back().first;
		};
		inline tGameTime getPosition() const {
			return mPosition;
		};
		inline unsigned getPositionIndex() const {
			return mPositionIndex;
		};
		inline unsigned getFrameCount() const {
			return mFrames.size();
		};

		static IDataObject * Load(const StringPool::Entry& sFilePath);
		DATA_LOADABLE(Replay, sizeof(Replay));
	};

	class ReplayStopEvent: public KeyedEvent<ReplayStopEvent> {
		Replay::tRef mrReplay;

	public:
		inline ReplayStopEvent(Replay::tRefa rReplay): mrReplay(rReplay) {};

		Replay::tRef getReplay() const {
			return mrReplay;
		};

		EVENT(ReplayStopEvent, KeyedEvent<ReplayStopEvent>, (mrReplay.getRef() - pEv->mrReplay.getRef()));
		GC_AUTOSIZE(ReplayStopEvent);
	};
};

#endif
